2
2
.
.
4
4
.
.
2
2
C
C
l
l
a
a
s
s
s
s
e
e
s
s
I
I
n
n
f
f
o
o
[
[
R
R
]
]
Classes
can contain Properties & Methods (add private Keyword to those that should not be accessible from the outside)
must have at least one constructor(...) Method to create its Instance
Content
Basic Class Syntax
Extend Class
Constructors (Parameters become Properties only if you use var or val.)
Companion Object (Workaround for implementing static Methods and Properties)
B
B
a
a
s
s
i
i
c
c
C
C
l
l
a
a
s
s
s
s
S
S
y
y
n
n
t
t
a
a
x
x
Class is declared by
using Keyword class class
followed by the Class Name Person
followed by the Class Body { ... }
which can have constructor constructor() (Method used to create Class Instance)
which can have Properties var name : String (Variables, Constants, Enumerators,...)
which can have Methods fun sayHello() { ... }
Basic Class Syntax
class Person { ... }
Basic Class Syntax
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person {
//DECLARE PROPERTIES.
public var name : String = "" //Default public Properties can be accessed from the outside
private var age : Int = 0 //Private Properties can only be accessed from Class Properties & Methods
//CONSTRUCTOR.
constructor(name: String, age: Int) {
this.name = name
this.age = age
}
//DECLARE METHODS.
fun sayHello() {
println("$name is $age years old")
}
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
var john = Person("John", 20) //Create Class Instance by calling its constructor() Method
john.sayHello() //John is 20 years old
print(john.name) //John
}
E
E
x
x
t
t
e
e
n
n
d
d
C
C
l
l
a
a
s
s
s
s
[
[
R
R
]
]
Child Class can extend single Parent Class by adding : ParentClassName after Child Class Name.
Use Keyword override to create Method that has the same signature as an existing Parent's one.
When overriding method with default parameter values, these values must be omitted from the signature.
Use Keyword super to access Parent's Properties and Methods super.displayName().
You can only extend from a Class or override a Method that were declared using Keyword open.
Extend Class Syntax
class Soldier : Person { ... }
Extend Class
//===========================================================================================================
// PARENT CLASS: Person
//===========================================================================================================
open class Person {
var name = ""
open fun displayName() { print(name) }
constructor(name: String) { this.name = name } //Secondary Constructor
}
//===========================================================================================================
// CHILD CLASS: Soldier
//===========================================================================================================
class Soldier : Person { //Extend from Person Class
override fun displayName() { println("Hello $name") } //Override Method
fun greet() { super.displayName() } //Call Parent's Method
constructor(name: String) : super(name) { println("Secondary Constructor") } //Call Parent's Constructor
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
//ACCESS PARENT FIELDS AND METHODS.
var john = Soldier("John") //Create Object from Class Soldier.
john.displayName() //Reference overriden method.
var name = (john.name) //Reference inherited field.
//DISPLAY NAME.
print(name)
}
C
C
o
o
n
n
s
s
t
t
r
r
u
u
c
c
t
t
o
o
r
r
s
s
[
[
R
R
]
]
[
[
R
R
]
]
Kotlin Class must have Constructor
Default Constructor is created by compiler if you don't explicitly declare any Constructor
Primary Constructor is declared before the body and its implementation is given inside init{} block
there can be only one Primary Constructor
you don't have to declare one but if there is one, Secondary Constructors must eventually call it
Secondary Constructor is declared inside the Body
you can have multiple Secondary Constructors with or without Primary Constructor
if there is Primary Constructor, Secondary Constructors must call it constructor(name: String) : this(name, 20) {
J
J
u
u
s
s
t
t
P
P
r
r
i
i
m
m
a
a
r
r
y
y
C
C
o
o
n
n
s
s
t
t
r
r
u
u
c
c
t
t
o
o
r
r
Input parameters of Primary Constructor are declared after Class Name and before the Class Body.
If you declare them with var or val they automatically become Class Properties
You can reference them with their name or inside the string with $name or ${name} (for complex expressions)
So just like in the Function you don't need to redeclare them inside the Class.
Additionally you can specify them to be private (like you can with any Class Property).
Without var or val they are just Constructor Parameters (and not Class Properties.)
Then you can only reference them inside init{} blocks and during Properties initialization (in the same way as above).
Actual implementation of Primary Constructor can be defined inside multiple init{} blocks and Properties Declarations
which are all executed in the given order (inside the Class Body as if representing a Function Body).
Using constructor keyword is optional. Without it declaration syntax looks more like that of a Function.
Primary Constructor Syntax
class Person constructor(public var name: String, age: Int) { ... init{} ... init{} ... } //Optional
class Person ( var name: String, val age: Int) { ... init{} ... init{} ... } //Properties
class Person ( name: String, age: Int) { ... init{} ... init{} ... } //Parameters
Just Primary Constructor
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person constructor(name: String = "", public var age: Int = 0) {
//PRIMARY CONSTRUCTOR IMPLEMENTATION.
init { println("name is $name") } //name is only Constructor Parameter
//DECLARE & INITIALIZE PROPERTIES.
var greet : String = "Hello $name" //Use Constructor Parameter name to initialize Property
//PRIMARY CONSTRUCTOR IMPLEMENTATION.
init { println("age is ${age}") } //age is both Constructor Parameter & Class Property
//DECLARE METHODS.
fun sayHello() {
println("$greet. You are ${age} years old") //Can't reference name since it is not Property
}
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
var john = Person("John", 20) //Create Class Instance by calling its constructor() Method
john.sayHello() //Hello John. You are 20 years old
print(john.age) //20 Because of var, age Parameter was created as Class Property
}
J
J
u
u
s
s
t
t
S
S
e
e
c
c
o
o
n
n
d
d
a
a
r
r
y
y
C
C
o
o
n
n
s
s
t
t
r
r
u
u
c
c
t
t
o
o
r
r
Secondary Constructor is declared inside the Body by using Keyword constructor.
It is automatically used if number and type of Parameters don't match Primary Constructor.
Secondary Constructor Syntax
constructor(name: String, age: Int) { ... }
Just Secondary Constructor
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person {
//DECLARE PROPERTIES.
public var name : String = "" //Default public Properties can be accessed from the outside
private var age : Int = 0 //Private Properties can only be accessed from Class Properties & Methods
//CONSTRUCTOR.
constructor(name: String, age: Int) {
this.name = name
this.age = age
}
//DECLARE METHODS.
fun sayHello() {
println("$name is $age years old")
}
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
//CREATE OBJECT.
var john = Person("John", 20) //Create Class Instance by calling its constructor() Method
john.sayHello() //John is 20 years old
print(john.name) //John
}
B
B
o
o
t
t
h
h
P
P
r
r
i
i
m
m
a
a
r
r
y
y
&
&
S
S
e
e
c
c
o
o
n
n
d
d
a
a
r
r
y
y
C
C
o
o
n
n
s
s
t
t
r
r
u
u
c
c
t
t
o
o
r
r
s
s
If both Primary and Secondary Constructors are defined, Secondary Constructor must call Primary Constructor before
executing its own Body by using this Keyword (highlighted in Red).
Since name is declared as Variable in Primary Constructor it automatically becomes Property (as in Functions).
Primary & Secondary Constructor Syntax
class Person(name: String, age: Int) {
init { ... }
constructor(name: String) : this(name, 20) { ... } //Call Primary Constructor before executing Secondary
}
Both Primary & Secondary Constructors
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person(public var name: String, age: Int) { //Primary constructor without constructor Keyword.
//DECLARE PROPERTIES.
private var age : Int = 0 //Private Properties can only be accessed from Class Properties & Methods
//PRIMARY CONSTRUCTOR IMPLEMENTATION.
init {
println("PRIMARY CONSTRUCTOR IMPLEMENTATION")
this.age = age
}
//SECONDARY CONSTRUCTOR.
constructor(name: String) : this(name, 20) {
println("SECONDARY CONSTRUCTOR")
}
//DECLARE METHODS.
fun sayHello() {
println("$name is $age years old")
}
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
//CREATE OBJECT.
var john = Person("John") //Create Class Instance by calling its constructor() Method
john.sayHello() //John is 20 years old
print(john.name) //John
}
C
C
o
o
m
m
p
p
a
a
n
n
i
i
o
o
n
n
O
O
b
b
j
j
e
e
c
c
t
t
[
[
R
R
]
]
Companion Object is a workaround to add static Methods & Properties to a Class (which Kotlin doesn't support).
They are accessed using Class name and are shared across all Class Instances.
A Companion Object is always declared inside of another Class with the default name of Companion.
Use @JvmStatic annotation on the Object's Methods & Properties that you want to make static for that Class.
Companion Object
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person {
companion object Companion {
@JvmStatic val name = "John"
@JvmStatic fun greet(age: Int) : String { return "$name is $age years old" }
}
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
var name = Person.name //Call static Property using Class name.
var result = Person.greet(20) //Call static Method using Class name.
println("Hello $name")
println(result)
}